package com.ruoyi.User.wxpay;

import org.apache.commons.lang3.StringUtils;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;



public class OtherUtils {

        /*
         * 生成32位随机字符串
         * */
        public static String getNonceStr() {
            UUID uuid = UUID.randomUUID();
            return uuid.toString().replace("-", "");
        }

        /**
         * 方法用途: 对所有传入参数按照字段名的Unicode码从小到大排序（字典序），并且生成url参数串<br>
         * 实现步骤: <br>
         *
         * @param paraMap    要排序的Map对象
         * @param urlEncode  是否需要URLENCODE
         * @param keyToLower 是否需要将Key转换为全小写
         *                   true:key转化成小写，false:不转化
         * @return
         */
        public static String formatUrlMap(Map<String, String> paraMap, boolean urlEncode, boolean keyToLower) {
            String buff = "";
            Map<String, String> tmpMap = paraMap;
            try {
                List<Map.Entry<String, String>> infoIds = new ArrayList<Map.Entry<String, String>>(tmpMap.entrySet());
                // 对所有传入参数按照字段名的 ASCII 码从小到大排序（字典序）
                Collections.sort(infoIds, new Comparator<Map.Entry<String, String>>() {
                    @Override
                    public int compare(Map.Entry<String, String> o1, Map.Entry<String, String> o2) {
                        return (o1.getKey()).toString().compareTo(o2.getKey());
                    }
                });
                // 构造URL 键值对的格式
                StringBuilder buf = new StringBuilder();
                for (Map.Entry<String, String> item : infoIds) {
                    if (StringUtils.isNotBlank(item.getKey())) {
                        String key = item.getKey();
                        String val = item.getValue();
                        if (urlEncode) {
                            val = URLEncoder.encode(val, "utf-8");
                        }
                        if (keyToLower) {
                            buf.append(key.toLowerCase() + "=" + val);
                        } else {
                            buf.append(key + "=" + val);
                        }
                        buf.append("&");
                    }

                }
                buff = buf.toString();
                if (buff.isEmpty() == false) {
                    buff = buff.substring(0, buff.length() - 1);
                }
            } catch (Exception e) {
                return null;
            }
            return buff;
        }



        /**
         * 功能描述: 判断值是否为double
         */
        public static boolean isToDouble(Object o){

            try {
                Double.valueOf(o.toString());
                return true;
            }catch (Exception e){
                return false;
            }
        }


        /**
         * @param xml
         * @return Map
         * @description 将xml字符串转换成map
         */
        public static Map<String, String> readStringXmlOut(String xml) {
            Map<String, String> map = new HashMap<String, String>();
            Document doc = null;
            try {
                doc = DocumentHelper.parseText(xml); // 将字符串转为XML
                Element rootElt = doc.getRootElement(); // 获取根节点
                @SuppressWarnings("unchecked")
                List<Element> list = rootElt.elements();// 获取根节点下所有节点
                for (Element element : list) { // 遍历节点
                    map.put(element.getName(), element.getText()); // 节点的name为map的key，text为map的value
                }
            } catch (DocumentException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return map;
        }


}

